package com.lupan.passwordbox.common.aop;

import com.lupan.passwordbox.common.base.Response;
import com.wordnik.swagger.annotations.ApiOperation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * 说明：操作日志打印
 *
 * @author lupan
 * @version 2017/8/18 11:20
 */
@Aspect
public class OperationLogAspect {

    @Pointcut("within(com.lupan.passwordbox.controller..*) && execution(public * *(..)) && @annotation(com.wordnik.swagger.annotations.ApiOperation)")
    private void canLog() {

    }

    @Pointcut("within(com.lupan.passwordbox.controller..*) && execution(public * *(..)) && @annotation(apiOperation)")
    private void canLogBindApiOperation(ApiOperation apiOperation){

    }

    @Pointcut("within(com.lupan.passwordbox.controller..*) && execution(public * *(..)) && @annotation(com.wordnik.swagger.annotations.ApiOperation) && args(id)")
    private void canLogBindId(long id){

    }

    @Before(value = "canLogBindId(id)", argNames = "id")
    private void before(long id) {

    }

    @AfterReturning(value = "canLog()", returning = "response")
    private void afterReturning(Response response) {

    }

    @AfterThrowing(value = "canLog()", throwing = "ex")
    private void afterThrowing(Exception ex) {

    }

    /**
     * 相当于finally，主要用于释放资源
     */
    @After("canLog()")
    private void after() {

    }

    @Around("canLog()")
    private Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        return result;
    }

    @Around(value = "within(com.lupan.passwordbox.controller..*) && execution(public * *(..)) && @annotation(apiOperation) && args(username,password)", argNames = "joinPoint,apiOperation,username,password")
    private Object around2(ProceedingJoinPoint joinPoint,ApiOperation apiOperation,String username,String password) throws Throwable {
        Object result = joinPoint.proceed(new Object[]{username,password});
        return result;
    }
}
